﻿using cuiail.DBConnections;
using cuiail.DBModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Data;

namespace cuiail.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class QualityIslandController : ControllerBase
    {
        private readonly IWorkingModeService _workingModeService;
        private readonly ICurrentWorkModeService _currentWorkModeService;
        private readonly IPrintContentService _printContentService;
        private readonly ILogger<LabController> _logger;

        public static _质检岛_POS_状态 qualityIslandStatus { get; set; } = new _质检岛_POS_状态();
        public static _质检岛_GET_命令 qualityIslandCommand { get; set; } = new _质检岛_GET_命令();


        public QualityIslandController(IWorkingModeService workingModeService,ICurrentWorkModeService currentWorkModeService, IPrintContentService printContentService, ILogger<LabController> logger)
        {
            _currentWorkModeService = currentWorkModeService;
            _workingModeService = workingModeService;
            _printContentService= printContentService;
            _logger = logger;
        }


        /// <summary>
        /// 获取命令数据
        /// </summary>
        [HttpGet]
        [Route("GetCommandData")]
        public async Task<ActionResult<IEnumerable<_质检岛_GET_命令>>> GetCommandDataAsync()
        {
            try
            {
                if (CloudPlatformController.cloudPlatformPosWorkOrder != null)
                {
                    
                   // qualityIslandCommand._当前生成产品信息 = CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable[0];
                    qualityIslandCommand._系统模式数据 = CloudPlatformController.cloudSystemModel.WorkingModeId;
                    qualityIslandCommand._系统命令 = CloudPlatformController.cloudSystemCommand.SystemCommand;
                    qualityIslandCommand._搬运机器人下料完成  = TransportRobotController.transportRobotStatus._当前工序状态;
                    //刷新质检岛的工序ID
                    qualityIslandCommand._工序ID=_工单管理类.AssignProcessIdByDeviceId(DeviceID.QualityInspectionIsland);
                }
                var result = qualityIslandCommand;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "get命令数据发生错误");
                return StatusCode(500, "get命令数据时发生错误，请稍后重试");
            }
        }


       
        /// <summary>
        /// 写入质检岛状态数据
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertQualityIslandStatusData")]
        public async Task<ActionResult<bool>> InsertQualityIslandStatusDataAsync(_质检岛_POS_状态 temp)
        {
            try
            {
                qualityIslandStatus = temp;
                CloudPlatformController.cloudInspectionIslandStatus.Updatefrom质检岛_POS_状态(qualityIslandStatus);
                _工单管理类._编号工序流转(CloudPlatformController.cloudWorkOrder, qualityIslandCommand._当前生成产品信息, temp._当前工序状态);
                TransportRobotController.transportRobotCommand._质检岛允许放料信号 = temp._搬运机器人允许放料信号_工序允许开始信号;
                var result = temp;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入质检岛状态数据时发生错误");
                return StatusCode(500, "写入质检岛状态数据时发生错误，请稍后重试");
            }
        }



        /// <summary>
        /// 清除当前生产信息
        /// </summary>
        /// <param name="">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("ClearCurrentProductionInfos")]
        public async Task<ActionResult<bool>> ClearCurrentProductionInfosAsync(int processID)
        {
            try
            {

                _工单管理类.ClearModeProcessListByWorkOrderId(processID);
                qualityIslandCommand._当前生成产品信息.ProductId="";
                return Ok(true);

            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入状态数据时发生错误");
                return StatusCode(500, "写入状态数据时发生错误，请稍后重试");
            }
        }







    }
}
